﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace MusicLyricApp.Core.Utils;

public static class HttpUtils
{
    /// <summary>
    /// 发起POST同步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="postData"></param>
    /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
    /// <param name="headers">填充消息头</param>        
    /// <returns></returns>
    public static string HttpPost(string url, string postData = null, string contentType = "application/json",
        int timeOut = 30, Dictionary<string, string> headers = null)
    {
        postData = postData ?? string.Empty;
        using (HttpClient client = new HttpClient())
        {
            if (headers != null)
            {
                foreach (var header in headers)
                    client.DefaultRequestHeaders.Add(header.Key, header.Value);
            }

            using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
            {
                if (contentType != null)
                    httpContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);

                HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
                return response.Content.ReadAsStringAsync().Result;
            }
        }
    }


    /// <summary>
    /// 发起POST异步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="postData"></param>
    /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
    /// <param name="headers">填充消息头</param>        
    /// <returns></returns>
    /// <exception cref="HttpRequestException"></exception>
    public static async Task<string> HttpPostAsync(string url, string postData = null,
        string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)
    {
        postData = postData ?? string.Empty;
        using (HttpClient client = new HttpClient())
        {
            client.Timeout = new TimeSpan(0, 0, timeOut);
            if (headers != null)
            {
                foreach (var header in headers)
                    client.DefaultRequestHeaders.Add(header.Key, header.Value);
            }

            using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
            {
                if (contentType != null)
                    httpContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);

                HttpResponseMessage response = await client.PostAsync(url, httpContent);
                return await response.Content.ReadAsStringAsync();
            }
        }
    }

    /// <summary>
    /// 发起GET同步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="headers"></param>
    /// <param name="contentType"></param>
    /// <returns></returns>
    /// <exception cref="HttpRequestException"></exception>
    public static string HttpGet(string url, string contentType = "application/json",
        Dictionary<string, string> headers = null)
    {
        var response = HttpGet0(url, contentType, headers);
        return response.Content.ReadAsStringAsync().Result;
    }

    public static HttpResponseMessage HttpGet0(string url, string contentType = "application/json",
        Dictionary<string, string> headers = null)
    {
        using (HttpClient client = new HttpClient())
        {
            if (contentType != null)
                client.DefaultRequestHeaders.Add("ContentType", contentType);
            if (headers != null)
            {
                foreach (var header in headers)
                    client.DefaultRequestHeaders.Add(header.Key, header.Value);
            }

            return client.GetAsync(url).Result;
        }
    }

    /// <summary>
    /// 发起GET异步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="headers"></param>
    /// <param name="contentType"></param>
    /// <returns></returns>
    public static async Task<string> HttpGetAsync(string url, string contentType = "application/json",
        Dictionary<string, string> headers = null)
    {
        using (HttpClient client = new HttpClient())
        {
            if (contentType != null)
                client.DefaultRequestHeaders.Add("ContentType", contentType);
            if (headers != null)
            {
                foreach (var header in headers)
                    client.DefaultRequestHeaders.Add(header.Key, header.Value);
            }

            HttpResponseMessage response = await client.GetAsync(url);
            return await response.Content.ReadAsStringAsync();
        }
    }


    /// <summary>
    /// 发起POST同步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="postData"></param>
    /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
    /// <param name="headers">填充消息头</param>        
    /// <returns></returns>
    public static T HttpPost<T>(string url, string postData = null, string contentType = "application/json",
        int timeOut = 30, Dictionary<string, string> headers = null)
    {
        return HttpPost(url, postData, contentType, timeOut, headers).ToEntity<T>();
    }


    /// <summary>
    /// 发起POST异步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="postData"></param>
    /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
    /// <param name="headers">填充消息头</param>        
    /// <returns></returns>
    public static async Task<T> HttpPostAsync<T>(string url, string postData = null,
        string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)
    {
        var res = await HttpPostAsync(url, postData, contentType, timeOut, headers);
        return res.ToEntity<T>();
    }

    /// <summary>
    /// 发起GET同步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="headers"></param>
    /// <param name="contentType"></param>
    /// <returns></returns>
    public static T HttpGet<T>(string url, string contentType = "application/json",
        Dictionary<string, string> headers = null)
    {
        return HttpGet(url, contentType, headers).ToEntity<T>();
    }

    /// <summary>
    /// 发起GET异步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="headers"></param>
    /// <param name="contentType"></param>
    /// <returns></returns>
    public static async Task<T> HttpGetAsync<T>(string url, string contentType = "application/json",
        Dictionary<string, string> headers = null)
    {
        var res = await HttpGetAsync(url, contentType, headers);
        return res.ToEntity<T>();
    }

    public static async Task<bool> DownloadFile(string url, string path)
    {
        try
        {
            var client = new HttpClient();

            using (var s = await client.GetStreamAsync(url))
            using (var fs = new FileStream(path, FileMode.OpenOrCreate))
            {
                await s.CopyToAsync(fs);
            }

            return true;
        }
        catch (Exception)
        {
            // ignore exception
            return false;
        }
    }
}